/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package mnstudent.presentationlayer;

import mnstudent.global.MyTableCellRender;
import mnstudent.global.DataTable;
import mnstudent.global.Standard;
import mnstudent.businessLayer.StateBLL;
import mnstudent.datatranferobject.StateDTO;
import java.awt.Dimension;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Vector;
import javax.swing.event.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author lee
 */
public class frmStatus extends javax.swing.JPanel {

    private StateBLL m_StateBLL;
    private int currentRow = 0;
    //private boolean m_isSave = false;
    private DefaultTableModel m_Model;
    /**
     * Creates new form frmState
     */
    public frmStatus() {
        initComponents();
        initControls();
        prepare();
    }

    private void initControls()
    {
        jxTxtStatusID.setEditable(false);//luon luon false
        jxtabDetails.setEditable(false);
         //neu chua them moi thi EditTable = false
        if(!this.jxtabDetails.m_isAdd && !this.jxtabDetails.m_isUpdate)
        {
            jxTxtNameOfStatus.setEditable(false);
            jxBtnSave.setEnabled(false);
            jxBtnCancel.setEnabled(false);

            jxBtnAdd.setEnabled(true);
            jxBtnUpdate.setEnabled(true);
            jxBtnDelete.setEnabled(true);
        }
        else
        {
            jxTxtNameOfStatus.setEditable(true);
            jxBtnSave.setEnabled(true);
            jxBtnCancel.setEnabled(true);
            

            jxBtnAdd.setEnabled(false);
            jxBtnUpdate.setEnabled(false);
            jxBtnDelete.setEnabled(false);
        }
    }
    private void prepare(){

        //TextField tinh/quan 
        jxTxtNameOfStatus.addKeyListener(new KeyAdapter() {
                public void keyTyped(KeyEvent e) {
                    int len = jxTxtNameOfStatus.getText().length();
                    if (len > Standard.MAX_LEN) {
                            e.consume();
                    }
                }
        });
        //Set lai text cac button cua dialog
        UIManager.put("OptionPane.cancelButtonText", "Hủy");
        UIManager.put("OptionPane.noButtonText", "Không");
        UIManager.put("OptionPane.okButtonText", "Có");
        UIManager.put("OptionPane.yesButtonText", "Vâng");
        //
        this.m_StateBLL = new StateBLL();
        m_Model = (DefaultTableModel)jxtabDetails.getModel();
        DataTable.addRow(m_StateBLL.getAllState(), m_Model);
        //get selected item
        jxtabDetails.setDefaultRenderer(Object.class, new MyTableCellRender());
        jxtabDetails.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent lse) {
               
                if(!(jxtabDetails.getSelectionModel().isSelectionEmpty( )))
                {
                    //retrieving the selected row index
                    currentRow = jxtabDetails.getSelectedRow();
                    //if a single row is selected from the table, take each cell values into the controls
                    if (jxtabDetails.getRowSelectionAllowed()&&jxtabDetails.getRowCount()>0) {
                        if(jxtabDetails.m_isAdd||jxtabDetails.m_isUpdate)
                        {
                            int rs = JOptionPane.showOptionDialog(null,
                                        "Vui lòng lưu thay đổi trước khi chuyển sang dòng khác!",
                                        "Cảnh báo",
                                        JOptionPane.YES_NO_OPTION,
                                        JOptionPane.QUESTION_MESSAGE,
                                        null,
                                        null,
                                        null);
                            if(rs == 0 )//Yes
                                save();
                            else
                            {
                                cancelAction();
                            }
                        }
                        else
                        {
                            String strStateID = jxtabDetails.getValueAt(currentRow, 0).toString();
                            String strNameOfState = jxtabDetails.getValueAt(currentRow, 1).toString();
                            jxTxtStatusID.setText(strStateID);
                            jxTxtNameOfStatus.setText(strNameOfState);
                        }
                    }
                }
            }
          });
        //neu co dl thi do mac dinh row dau tien
        if(jxtabDetails.getRowCount()>0)
        {
            jxtabDetails.setRequestFocusEnabled(true);
            jxtabDetails.requestFocus();
            jxtabDetails.changeSelection(jxtabDetails.getRowCount()-1, jxtabDetails.getRowCount()-1, false, false);
        }
    }
    /**
     * Ham thuc hien insert Khoa xuong database
     * @return 
     */
    private void saveToDatabase()
    {

        if(!this.jxTxtNameOfStatus.getText().equals(""))
        {    
            String id = "KH0000***";
            String name = Standard.standardName(this.jxTxtNameOfStatus.getText());
            StateDTO facultyObj = new StateDTO(id, name);
            if(m_StateBLL.insertStateToDatabase(facultyObj)){
                JOptionPane.showOptionDialog(null, 
                        "Lưu xuống thành công!", 
                        "Thông báo", 
                        JOptionPane.YES_OPTION,
                        JOptionPane.QUESTION_MESSAGE,
                        null,
                        null,
                        null);
                 //neu luu thanh cong
                this.jxtabDetails.m_isAdd = false;
                initControls();
                refreshDataInJTable();
            }
            else
                 JOptionPane.showMessageDialog(null, "Lưu xuống thất bại. Vui lòng kiểm tra lại!", "Lỗi", JOptionPane.YES_OPTION);
        }
    }

    /**
     * Ham thuc hien cap nhat Khoa
     * @return 
     */
    private void updateToDatabase()
    {
        if(checkData())
        {    
            String id = this.jxTxtStatusID.getText().trim();
            String name = Standard.standardName(this.jxTxtNameOfStatus.getText());
            StateDTO facultyObj = new StateDTO(id, name);
            if(m_StateBLL.updateStateToDatabase(facultyObj)){
               JOptionPane.showOptionDialog(null, 
                     "Câp nhật thành công!", 
                     "Thông báo", 
                     JOptionPane.YES_OPTION,
                     JOptionPane.QUESTION_MESSAGE,
                     null,
                     null,
                     null);
                     //neu Cập nhật thanh cong
                    this.jxtabDetails.m_isUpdate = false;
                    initControls();
                    refreshDataInJTable();
                }
                else
                 JOptionPane.showMessageDialog(null, "Cập nhật thất bại. Vui lòng kiểm tra lại!", "Lỗi", JOptionPane.YES_OPTION);
        }
       
    }
    /**
     * Ham xoa 1 khoa dc chon
     * @param rowSelected
     * @return 
     */
     private boolean isDelete(int rowSelected)
    {
        boolean m_isDelete = false;

        String id = this.jxtabDetails.getValueAt(rowSelected, 0).toString();
        String name = this.jxtabDetails.getValueAt(rowSelected, 1).toString();
        StateDTO facultyObj = new StateDTO(id, name);
        if(m_StateBLL.deleteStateFromDatabase(facultyObj)){
            m_Model.removeRow(rowSelected);
            m_isDelete = true;
        }

        return m_isDelete;
    }
     /**
      * Ham xoa 1 luc nhieu khoa
      * @return 
      */
    private boolean isDeleteMoreState()
    {
        boolean m_isDelete = true;
        int[] arrRowSelected = this.jxtabDetails.getSelectedRows();
        for(int i = arrRowSelected.length - 1; i >=0; i--)
        {
            if(!isDelete(arrRowSelected[i])){
                JOptionPane.showMessageDialog(null, 
                        "Xóa thất bại khoa: "+ jxtabDetails.getValueAt(arrRowSelected[i], 1) , 
                        "Lỗi", 
                        JOptionPane.YES_OPTION);
                m_isDelete = false;
            }
        }
        return m_isDelete;
    }
    /**
     * Ham thuc hien viec cap nhat va update
     */
     private void save()
    {
         if(this.jxtabDetails.m_isAdd)
        {
            saveToDatabase();
               
        }
        else
            if(this.jxtabDetails.m_isUpdate)
            {
               updateToDatabase();
            }
    }
     
     private void cancelAction()
     {
         if(this.jxtabDetails.m_isAdd){
            ((DefaultTableModel)jxtabDetails.getModel()).removeRow(jxtabDetails.getRowCount()-1);
            this.jxtabDetails.m_isAdd = false;
            initControls();
            jxTxtStatusID.setText("");
            jxTxtNameOfStatus.setText("");
            jxtabDetails.changeSelection(jxtabDetails.getRowCount()-1, jxtabDetails.getRowCount()-1, false, false);
        }
        else
        {
           this.jxtabDetails.m_isUpdate = false;
           initControls(); 
        }
        refreshDataInJTable();
     }
     
     private void refreshDataInJTable()
    {
        m_Model.fireTableDataChanged();
        m_Model.fireTableStructureChanged();
        m_Model.getDataVector().removeAllElements();
        DataTable.addRow(m_StateBLL.getAllState(), (DefaultTableModel)jxtabDetails.getModel());
        if(jxtabDetails.getRowCount()>0)
        {
            jxtabDetails.setRequestFocusEnabled(true);
            jxtabDetails.requestFocus();
            jxtabDetails.changeSelection(jxtabDetails.getRowCount()-1, jxtabDetails.getRowCount()-1, false, false);
        }
    }
     
    private boolean checkData()
     {
           if(this.jxTxtNameOfStatus.getText().equals("")){
            JOptionPane.showMessageDialog(null, "Tên trạng thái không được bỏ trống!");
            return false;
        }
        else
        {
            if(!m_StateBLL.checkInputIsInvalid(this.jxtabDetails, this.jxTxtNameOfStatus.getText()))
            {
                JOptionPane.showMessageDialog(null, "Trạng thái vừa nhập đã có trong cơ sở dữ liệu!");
                return false;
            }
        }
        return true;
         
     }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jxpanDetails = new org.jdesktop.swingx.JXPanel();
        jpanTableDetails = new javax.swing.JPanel();
        jpanContent = new javax.swing.JPanel();
        jxpanFindNavibar = new org.jdesktop.swingx.JXPanel();
        jpanChose = new javax.swing.JPanel();
        jChkSearchWithFacultyID = new javax.swing.JCheckBox();
        jChkSearchWithFacultyName = new javax.swing.JCheckBox();
        jChkSearchFast = new javax.swing.JCheckBox();
        jxbtnFindVisible = new org.jdesktop.swingx.JXButton();
        jxSearchFaculty = new org.jdesktop.swingx.JXSearchField();
        jXLabel1 = new org.jdesktop.swingx.JXLabel();
        jScrollPane = new javax.swing.JScrollPane();
        jxtabDetails = new mnstudent.global.MyTable();
        jpanFuntionalDetails = new javax.swing.JPanel();
        jxBtnAdd = new org.jdesktop.swingx.JXButton();
        jxBtnUpdate = new org.jdesktop.swingx.JXButton();
        jxBtnDelete = new org.jdesktop.swingx.JXButton();
        jxBtnSave = new org.jdesktop.swingx.JXButton();
        jxBtnCancel = new org.jdesktop.swingx.JXButton();
        jxpanHeader = new org.jdesktop.swingx.JXPanel();
        jxpanTitleHeader = new org.jdesktop.swingx.JXPanel();
        jXLabel5 = new org.jdesktop.swingx.JXLabel();
        jxpanInfoHeader = new org.jdesktop.swingx.JXPanel();
        jxpanInfo = new org.jdesktop.swingx.JXPanel();
        jxLabFacultyID = new org.jdesktop.swingx.JXLabel();
        jxTxtStatusID = new org.jdesktop.swingx.JXTextField();
        jxLabNameOfFaculty = new org.jdesktop.swingx.JXLabel();
        jxTxtNameOfStatus = new org.jdesktop.swingx.JXTextField();

        setLayout(new java.awt.BorderLayout());

        jxpanDetails.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jxpanDetails.setLayout(new java.awt.BorderLayout());

        jpanTableDetails.setLayout(new java.awt.BorderLayout());

        jpanContent.setLayout(new java.awt.BorderLayout());

        jxpanFindNavibar.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jxpanFindNavibar.setPreferredSize(new java.awt.Dimension(914, 30));

        jpanChose.setBorder(javax.swing.BorderFactory.createTitledBorder("Chọn nội dung tìm kiếm"));

        jChkSearchWithFacultyID.setText("Tìm theo mã trạng thái");

        jChkSearchWithFacultyName.setText("Tìm theo tên trạng thái");

        jChkSearchFast.setText("Tìm kiếm nhanh");

        javax.swing.GroupLayout jpanChoseLayout = new javax.swing.GroupLayout(jpanChose);
        jpanChose.setLayout(jpanChoseLayout);
        jpanChoseLayout.setHorizontalGroup(
            jpanChoseLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpanChoseLayout.createSequentialGroup()
                .addGap(14, 14, 14)
                .addComponent(jChkSearchWithFacultyID)
                .addGap(54, 54, 54)
                .addComponent(jChkSearchWithFacultyName)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 284, Short.MAX_VALUE)
                .addComponent(jChkSearchFast)
                .addGap(105, 105, 105))
        );
        jpanChoseLayout.setVerticalGroup(
            jpanChoseLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpanChoseLayout.createSequentialGroup()
                .addGroup(jpanChoseLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jChkSearchWithFacultyID)
                    .addComponent(jChkSearchFast)
                    .addComponent(jChkSearchWithFacultyName))
                .addGap(0, 0, 0))
        );

        jxbtnFindVisible.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        jxbtnFindVisible.setText("Tùy chọn");
        jxbtnFindVisible.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jxbtnFindVisibleActionPerformed(evt);
            }
        });

        jXLabel1.setText("Tìm kiếm:");

        javax.swing.GroupLayout jxpanFindNavibarLayout = new javax.swing.GroupLayout(jxpanFindNavibar);
        jxpanFindNavibar.setLayout(jxpanFindNavibarLayout);
        jxpanFindNavibarLayout.setHorizontalGroup(
            jxpanFindNavibarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jxpanFindNavibarLayout.createSequentialGroup()
                .addComponent(jxbtnFindVisible, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jXLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jxSearchFaculty, javax.swing.GroupLayout.PREFERRED_SIZE, 219, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addComponent(jpanChose, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jxpanFindNavibarLayout.setVerticalGroup(
            jxpanFindNavibarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jxpanFindNavibarLayout.createSequentialGroup()
                .addGroup(jxpanFindNavibarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jxbtnFindVisible, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jxSearchFaculty, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jXLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jpanChose, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jpanContent.add(jxpanFindNavibar, java.awt.BorderLayout.NORTH);

        jxtabDetails.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Mã trạng thái", "Tên trạng thái"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jxtabDetails.setSelectionBackground(new java.awt.Color(153, 153, 153));
        jScrollPane.setViewportView(jxtabDetails);
        if (jxtabDetails.getColumnModel().getColumnCount() > 0) {
            jxtabDetails.getColumnModel().getColumn(0).setResizable(false);
        }

        jpanContent.add(jScrollPane, java.awt.BorderLayout.CENTER);

        jpanTableDetails.add(jpanContent, java.awt.BorderLayout.CENTER);

        jpanFuntionalDetails.setLayout(new java.awt.GridBagLayout());

        jxBtnAdd.setText("Thêm mới");
        jxBtnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jxBtnAddActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 20;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(8, 10, 6, 9);
        jpanFuntionalDetails.add(jxBtnAdd, gridBagConstraints);

        jxBtnUpdate.setText("Sửa đổi");
        jxBtnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jxBtnUpdateActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 20;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTH;
        gridBagConstraints.insets = new java.awt.Insets(8, 10, 6, 8);
        jpanFuntionalDetails.add(jxBtnUpdate, gridBagConstraints);

        jxBtnDelete.setText("Xóa bỏ");
        jxBtnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jxBtnDeleteActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 20;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 4, 8);
        jpanFuntionalDetails.add(jxBtnDelete, gridBagConstraints);

        jxBtnSave.setText("Lưu xuống");
        jxBtnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jxBtnSaveActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 20;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(11, 9, 7, 8);
        jpanFuntionalDetails.add(jxBtnSave, gridBagConstraints);

        jxBtnCancel.setText("Hủy bỏ");
        jxBtnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jxBtnCancelActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 20;
        gridBagConstraints.ipady = 6;
        gridBagConstraints.insets = new java.awt.Insets(12, 9, 8, 9);
        jpanFuntionalDetails.add(jxBtnCancel, gridBagConstraints);

        jpanTableDetails.add(jpanFuntionalDetails, java.awt.BorderLayout.EAST);

        jxpanDetails.add(jpanTableDetails, java.awt.BorderLayout.CENTER);

        add(jxpanDetails, java.awt.BorderLayout.CENTER);

        jxpanHeader.setLayout(new java.awt.BorderLayout());

        jxpanTitleHeader.setBorder(new org.jdesktop.swingx.border.DropShadowBorder());

        jXLabel5.setForeground(new java.awt.Color(0, 102, 255));
        jXLabel5.setText("TRẠNG THÁI SINH VIÊN");
        jXLabel5.setFont(new java.awt.Font("Times New Roman", 1, 24)); // NOI18N
        jxpanTitleHeader.add(jXLabel5);

        jxpanHeader.add(jxpanTitleHeader, java.awt.BorderLayout.PAGE_START);

        jxpanInfoHeader.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jxpanInfoHeader.setLayout(new java.awt.BorderLayout());

        jxpanInfo.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "THÔNG TIN", 1, 2));

        jxLabFacultyID.setText("Mã trạng thái");

        jxTxtStatusID.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                jxTxtStatusIDCaretUpdate(evt);
            }
        });

        jxLabNameOfFaculty.setText("Tên trạng thái");

        jxTxtNameOfStatus.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                jxTxtNameOfStatusCaretUpdate(evt);
            }
        });
        jxTxtNameOfStatus.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jxTxtNameOfStatusKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout jxpanInfoLayout = new javax.swing.GroupLayout(jxpanInfo);
        jxpanInfo.setLayout(jxpanInfoLayout);
        jxpanInfoLayout.setHorizontalGroup(
            jxpanInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jxpanInfoLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jxLabFacultyID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jxTxtStatusID, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(50, 50, 50)
                .addComponent(jxLabNameOfFaculty, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(29, 29, 29)
                .addComponent(jxTxtNameOfStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 234, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(315, Short.MAX_VALUE))
        );
        jxpanInfoLayout.setVerticalGroup(
            jxpanInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jxpanInfoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jxpanInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jxLabFacultyID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jxTxtStatusID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jxLabNameOfFaculty, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jxTxtNameOfStatus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jxpanInfoHeader.add(jxpanInfo, java.awt.BorderLayout.CENTER);

        jxpanHeader.add(jxpanInfoHeader, java.awt.BorderLayout.CENTER);

        add(jxpanHeader, java.awt.BorderLayout.NORTH);
    }// </editor-fold>//GEN-END:initComponents

    private void jxbtnFindVisibleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jxbtnFindVisibleActionPerformed
        if(jxbtnFindVisible.getText().equals("Đóng"))
        {
          jxbtnFindVisible.setPreferredSize(new Dimension(110,25));
          jxbtnFindVisible.setText("Tùy chọn");
          jxbtnFindVisible.revalidate();
          jxpanFindNavibar.setPreferredSize(new Dimension(30, 30));
          jxpanFindNavibar.revalidate();
        }
        else
        {
            jxbtnFindVisible.setPreferredSize(new Dimension(20, 25));
            jxbtnFindVisible.setText("Đóng");
            jxbtnFindVisible.revalidate();
            jxpanFindNavibar.setPreferredSize(new Dimension(30, 105));
            jxpanFindNavibar.revalidate();
        }
    }//GEN-LAST:event_jxbtnFindVisibleActionPerformed

    private void jxBtnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jxBtnAddActionPerformed
        
        for (int i = 0; i < jxtabDetails.getColumnCount(); i++) {
            if(jxtabDetails.getValueAt(jxtabDetails.getRowCount()-1, i).equals(""))
                return;
        }

        Vector newRow = new Vector(jxtabDetails.getColumnCount());
        for (int i = 0; i < jxtabDetails.getColumnCount(); i++) {
            newRow.addElement("");
        }
        ((DefaultTableModel)jxtabDetails.getModel()).addRow(newRow);
        jxtabDetails.setRowSelectionInterval(jxtabDetails.getRowCount()-1,jxtabDetails.getRowCount()-1);
        //jxtabDetails.setEnabled(false);
        //jxtabDetails.setRowSelectionAllowed(false);
        JScrollBar sb = jScrollPane.getVerticalScrollBar();
        sb.setValue( sb.getMaximum());
        
        if(!this.jxtabDetails.m_isAdd){           
           this.jxtabDetails.m_isAdd = true;
           initControls();
           this.jxtabDetails.m_rowIndex = jxtabDetails.getRowCount()-1;
           jxTxtStatusID.setText("TT0000***");
           jxTxtNameOfStatus.requestFocus();
        }
    }//GEN-LAST:event_jxBtnAddActionPerformed

    private void jxTxtStatusIDCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_jxTxtStatusIDCaretUpdate
       if(this.jxtabDetails.m_isAdd)
            jxtabDetails.getModel().setValueAt(jxTxtStatusID.getText(), jxtabDetails.getRowCount()-1, 0);
       else
           if(this.jxtabDetails.m_isUpdate)
               jxtabDetails.getModel().setValueAt(jxTxtStatusID.getText(), jxtabDetails.getSelectedRow(), 0);
    }//GEN-LAST:event_jxTxtStatusIDCaretUpdate

    private void jxTxtNameOfStatusCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_jxTxtNameOfStatusCaretUpdate
        if(this.jxtabDetails.m_isAdd)
            jxtabDetails.getModel().setValueAt(jxTxtNameOfStatus.getText(), jxtabDetails.getRowCount()-1, 1);
        else
            if(this.jxtabDetails.m_isUpdate)
                jxtabDetails.getModel().setValueAt(jxTxtNameOfStatus.getText(), jxtabDetails.getSelectedRow(), 1);
    }//GEN-LAST:event_jxTxtNameOfStatusCaretUpdate

    private void jxBtnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jxBtnSaveActionPerformed

       save();
    }//GEN-LAST:event_jxBtnSaveActionPerformed

    private void jxBtnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jxBtnCancelActionPerformed
       
        cancelAction();
    }//GEN-LAST:event_jxBtnCancelActionPerformed

    private void jxBtnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jxBtnUpdateActionPerformed
        jxBtnAdd.setEnabled(false);
        jxBtnDelete.setEnabled(false);
        jxBtnUpdate.setEnabled(false);
        
        jxBtnSave.setEnabled(true);
        jxBtnCancel.setEnabled(true);
        jxTxtNameOfStatus.setEditable(true);
        jxTxtNameOfStatus.requestFocus();
        
        this.jxtabDetails.m_isUpdate = true;      
    }//GEN-LAST:event_jxBtnUpdateActionPerformed

    private void jxBtnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jxBtnDeleteActionPerformed
       
        int rs = JOptionPane.showOptionDialog(null, 
                        "Bạn có muốn xóa không!", 
                        "Câu hỏi", 
                        JOptionPane.YES_NO_OPTION,
                        JOptionPane.QUESTION_MESSAGE,
                        null,
                        null,
                        null);
        if(rs == 0)
        {
            if(isDeleteMoreState())
            {
                JOptionPane.showOptionDialog(null, 
                        "Xóa thành công!", 
                        "Thông báo", 
                        JOptionPane.YES_OPTION,
                        JOptionPane.INFORMATION_MESSAGE,
                        null,
                        null,
                        null);
                 //neu luu thanh cong
                this.jxtabDetails.m_isAdd = false;
                initControls();
                refreshDataInJTable();
            }
            else
                 JOptionPane.showMessageDialog(null, "Xóa thất bại. Vui lòng kiểm tra lại!", "Lỗi", JOptionPane.YES_OPTION);
        }
    }//GEN-LAST:event_jxBtnDeleteActionPerformed

    private void jxTxtNameOfStatusKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jxTxtNameOfStatusKeyPressed
        if(evt.getKeyCode() == KeyEvent.VK_ENTER)
        {
            save();
        }
        
        if(jxTxtNameOfStatus.getText().length()>Standard.MAX_LEN)
            jxTxtNameOfStatus.setText(jxTxtNameOfStatus.getText().substring(0, Standard.MAX_LEN));
    }//GEN-LAST:event_jxTxtNameOfStatusKeyPressed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox jChkSearchFast;
    private javax.swing.JCheckBox jChkSearchWithFacultyID;
    private javax.swing.JCheckBox jChkSearchWithFacultyName;
    private javax.swing.JScrollPane jScrollPane;
    private org.jdesktop.swingx.JXLabel jXLabel1;
    private org.jdesktop.swingx.JXLabel jXLabel5;
    private javax.swing.JPanel jpanChose;
    private javax.swing.JPanel jpanContent;
    private javax.swing.JPanel jpanFuntionalDetails;
    private javax.swing.JPanel jpanTableDetails;
    private org.jdesktop.swingx.JXButton jxBtnAdd;
    private org.jdesktop.swingx.JXButton jxBtnCancel;
    private org.jdesktop.swingx.JXButton jxBtnDelete;
    private org.jdesktop.swingx.JXButton jxBtnSave;
    private org.jdesktop.swingx.JXButton jxBtnUpdate;
    private org.jdesktop.swingx.JXLabel jxLabFacultyID;
    private org.jdesktop.swingx.JXLabel jxLabNameOfFaculty;
    private org.jdesktop.swingx.JXSearchField jxSearchFaculty;
    private org.jdesktop.swingx.JXTextField jxTxtNameOfStatus;
    private org.jdesktop.swingx.JXTextField jxTxtStatusID;
    private org.jdesktop.swingx.JXButton jxbtnFindVisible;
    private org.jdesktop.swingx.JXPanel jxpanDetails;
    private org.jdesktop.swingx.JXPanel jxpanFindNavibar;
    private org.jdesktop.swingx.JXPanel jxpanHeader;
    private org.jdesktop.swingx.JXPanel jxpanInfo;
    private org.jdesktop.swingx.JXPanel jxpanInfoHeader;
    private org.jdesktop.swingx.JXPanel jxpanTitleHeader;
    private mnstudent.global.MyTable jxtabDetails;
    // End of variables declaration//GEN-END:variables
}
